package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import java.util.Objects;

import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.UpToDateDealingAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.util.CalculateExpireTimeUtil;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;

/**
 * 趸交资费——到期处理方式属性联动处理类
 */
@Service
@AttrHandler(UpToDateDealingAttrHandler.class)
public class UpToDateDealingAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        //联动处理失效时间
        this.dealExpireTime(attr, inst, data);
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr rentAfterUpToDateAttr = inst.getAttr("210004552");
        if (LocalKeyConsts.END_UP_IN_PERIOD_MONTHLY_RENT.equals(attr.getValue())) {
            rentAfterUpToDateAttr.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
            rentAfterUpToDateAttr.setIsNull(LocalKeyConsts.IFTRUE_F);
        }
        else {
            rentAfterUpToDateAttr.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
            rentAfterUpToDateAttr.setIsNull(LocalKeyConsts.IFTRUE_T);
        }
    }

    /**
     * 联动处理失效时间
     * @param attr
     * @param inst
     * @param data
     */
    private void dealExpireTime(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr endDateAttr = inst.getAttr(LocalKeyConsts.END_DATE_FIELD_NAME);
        KiteAttr periodMonthAttr = inst.getAttr("200003242");
        if (Objects.isNull(endDateAttr) || KiteStringUtils.isEmpty(endDateAttr.getValue())) {
            return;
        }
        if (KiteStringUtils.equalsAny(attr.getValue(), LocalKeyConsts.END_UP_IN_PERIOD_AUTO_RENEW, LocalKeyConsts.END_UP_IN_PERIOD_MONTHLY_RENT)) {
            endDateAttr.setValue("2029-12-31 23:59:59");
            endDateAttr.setValueDesc("2029-12-31 23:59:59");
        }
        if (KiteStringUtils.equalsAny(attr.getValue(), LocalKeyConsts.END_UP_IN_PERIOD_TERMINATED, LocalKeyConsts.END_UP_IN_PERIOD_STOPPED) && Objects.nonNull(periodMonthAttr)) {
            CalculateExpireTimeUtil.dealExpireTime(periodMonthAttr, inst, data);
        }
    }
}
